from Tkinter import *
import Tkinter
import tkMessageBox

import aim.aimClient
from aim.flap import *
import aim.vars
import hashlib

class login_prompt(Frame):
    
    def __init__(self,master=None):

        self.pw = None
        self.sn = None
        self.win = Toplevel(master)
        Frame.__init__(self, self.win)
        """Set the Window Title"""
        self.master.title("Aim")
        self.top = self.winfo_toplevel()

        ws = self.winfo_screenwidth()
        hs = self.winfo_screenheight()
        self.win.geometry("%dx%d%+d%+d" % (250, 100, ws/2 - 125 , hs/2 - 50))
        """Display the main window"
        with a little bit of padding"""
        self.grid(padx=2, pady=2,sticky=N+S+E+W)
        self.InitResizing()
        self.CreateWidgets()
    
    def InitResizing(self):        
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(1, weight=1)
        top.columnconfigure(2, weight=1)
        self.columnconfigure(1, weight=1)
        self.columnconfigure(2, weight=1)


    def CreateWidgets(self):
        """Create all the widgests that we need"""
                       
        """Create the Text"""
        
        self.userLabel = Label(self, text="User Name:")
        self.passLabel = Label(self, text="Password:")
        self.userName = Entry(self, width=15)
        self.password = Entry(self, width=15, show="*")

        self.userLabel.grid(row=0, column=0,columnspan=1, sticky=W)
        self.passLabel.grid(row=1, column=0,columnspan=1, sticky=W)
        self.userName.grid(row=0, column=1,sticky=E)
        self.password.grid(row=1, column=1, sticky=E)

        self.offlineBtn = Button(self, text="Use Offline", command=self.startOffline)
        self.connectBtn = Button(self, text="Connect", command=self.connect)
    
        self.offlineBtn.grid(column=0, row=2, stick=E, pady=5)
        self.connectBtn.grid(column=1, row=2, stick=E, pady=5)
        self.password.bind('<Return>', self.connect)
        

    def connect(self, event=None):        
        self.pw = hashlib.md5(self.password.get()).digest()
        self.sn = self.userName.get()
        self.top.destroy()
        self.contactAim(self.sn, self.pw)
        
    
    def startOffline(self, event=None):
        self.pw = None
        self.sn = None
        self.top.destroy()

    def contactAim(self, sn, pw):
        vars.connected = False
        error, BOS, cookie, email, passchange = aim.aimClient.authorize("login.oscar.aol.com", 5190, sn, pw)
    
        if not (error == None):
            tkMessageBox.showwarning("Error", error)
            vars.connectivity.set("AIM Connection Failure")
            return False, None
        else:
            print "Authorized User"
            vars.stream = aim.aimClient.negotiate(BOS, cookie, email, passchange)           
            aim.aimClient.activate(vars.stream)
            print "Getting initial crap"
            while(1):
                r = flap()
                try:
                    aim.aimClient.get(vars.stream, r)
                    print r
                    sleep(.15)
                except:
                    break
            print "Done getting initialization crap, real things after this"
            vars.connected = True
            print vars.connected, vars.stream
            vars.connectivity.set("AIM "+self.sn)


if __name__ == "__main__":
    print "Not a main module"
    root = Tk()
    vars.connected = False
    log = login_prompt(root)

    print vars.connected
    while(1):
        log.update()
        response = ""
        r = flap()
        if vars.connected:
            try:       
                modules.aim.aimClient.get(vars.stream, r)
                if(r.SNAC.family == 4 and r.SNAC.subtype == 7):
                    print 'Yay!'
                
            except:
                pass
            
